package com.capgemini.fs.app.ekms.persistence.util;

import java.util.Collection;
import java.util.List;

public class QueryHelper {
	StringBuffer	selectBuffer	= new StringBuffer("select ");
	StringBuffer	fromBuffer		= new StringBuffer("from ");
	StringBuffer	whereBuffer		= new StringBuffer("where 1=1 ");
	StringBuffer	groupByBuffer	= new StringBuffer(" Group by ");
	StringBuffer	havingBuffer	= new StringBuffer(" Having ");
	StringBuffer	hqlBuffer		= new StringBuffer();

	public StringBuffer selectField(String column) {
		return selectBuilder(column + " ");
	}

	public StringBuffer selectField(String column, String alias) {
		return selectBuilder(column + " as " + alias + " ");
	}

	public StringBuffer selectDistinctField(String column) {
		return selectBuilder(" distinct " + column + " ");
	}

	public StringBuffer createAlias(String pojoName, String alias) {
		return this.createAlias(pojoName, alias, false);
	}

	public StringBuffer createAlias(String pojoName, String alias,
			boolean isSubquery) {
		if (isSubquery) {
			return fromBuilder("(" + pojoName + ")" + " as " + alias + " ");
		} else {
			return fromBuilder(pojoName + " as " + alias + " ");
		}
	}

	public StringBuffer innerJoin(String pojoName, String alias) {

		return this.innerJoin(pojoName, alias, false);
	}

	public StringBuffer innerJoin(String pojoName, String alias,
			boolean isSubquery) {
		if (isSubquery) {
			return fromBuilder(" inner join " + "(" + pojoName + ")" + " as "
					+ alias + " ");
		} else {
			return fromBuilder(" inner join " + pojoName + " as " + alias + " ");
		}

	}

	public StringBuffer leftJoin(String pojoName, String alias) {
		String hql = " left join " + pojoName + " as " + alias + " ";
		return fromBuilder(hql);
	}

	public StringBuffer groupBy(String query) {
		return groupByBuilder(query);
	}

	public StringBuffer having(String query) {
		return havingBuilder(query);
	}

	public StringBuffer addRestriction(String str) {
		return whereBuilder(str);
	}

	public StringBuffer selectField(String[] columns) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < columns.length; i++) {
			sb.append(columns[i]);
			if (i < columns.length - 1) {
				sb.append(",");
			}
		}
		return sb;
	}

	private StringBuffer selectBuilder(String query) {
		return selectBuffer.append(query);
	}

	private StringBuffer fromBuilder(String query) {
		return fromBuffer.append(query);
	}

	private StringBuffer whereBuilder(String query) {
		return whereBuffer.append(query);
	}

	private StringBuffer groupByBuilder(String query) {
		return groupByBuffer.append(query);
	}

	private StringBuffer havingBuilder(String query) {
		return havingBuffer.append(query);
	}

	public StringBuffer queryBuilder() {
		StringBuffer hql = hqlBuffer.append(selectBuffer).append(fromBuffer)
				.append(whereBuffer);
		// .append(groupByBuffer)
		// .append(havingBuffer);
		if (!groupByBuffer.toString().trim().equals("Group by")) {
			hql.append(groupByBuffer);
		}
		if (!havingBuffer.toString().trim().equals("Having")) {
			hql.append(havingBuffer);
		}

		selectBuffer = new StringBuffer("select ");
		fromBuffer = new StringBuffer("from ");
		whereBuffer = new StringBuffer("where 1=1 ");
		groupByBuffer = new StringBuffer(" Group by ");
		havingBuffer = new StringBuffer(" Having ");
		hqlBuffer = new StringBuffer();

		return hql;
	}

	/**
	 * Justify the object inputed is null or empty string.
	 * 
	 * @param obj
	 * @return boolean
	 */
	public static boolean isNullOrEmpty(Object obj) {
		if (null == obj) {
			return true;
		}
		return false;
	}

	public static class Restriction {
		public String like(String pojo, String value) {
			return " and " + pojo + " like '" + value + "' ";
		}

		public String le(String pojo, String value) {
			return " and " + pojo + " <= '" + value + "' ";
		}

		public String lt(String pojo, String value) {
			return " and " + pojo + " < '" + value + "' ";
		}

		public String ge(String pojo, String value) {
			return " and " + pojo + " >= '" + value + "' ";
		}

		public String gt(String pojo, String value) {
			return " and " + pojo + " > '" + value + "' ";
		}

		public String eq(String pojo, String value) {
			return " and " + pojo + " = " + value;
		}

		public String eq(String pojo, int value) {
			return " and " + pojo + " = " + value;
		}

		@SuppressWarnings("unchecked")
		public String in(String pojo, List list) {
			StringBuffer sb = new StringBuffer();
			sb.append(" and ");
			sb.append(pojo);
			sb.append(" in(");
			if (list != null && !list.isEmpty()) {
				for (int i = 0; i < list.size(); i++) {

					if (list.get(i) instanceof String) {
						sb.append("'");
						sb.append(list.get(i));
						sb.append("'");
					} else {
						sb.append(list.get(i));
					}
					if (i < list.size() - 1) {
						sb.append(",");
					}
				}
			}
			sb.append(") ");
			return sb.toString();
		}

		public String in(String field, String subQuery) {
			return " and " + field + " in(" + subQuery + ")";
		}
	}

	public static boolean isNullOrEmpty(String obj) {
		if (null == obj) {
			return true;
		}
		if ("".equals(obj)) {
			return true;
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	public static boolean isNullOrEmpty(Collection collection) {
		if (null == collection) {
			return true;
		}
		if (collection.isEmpty()) {
			return true;
		}
		return false;
	}

}
